const userService = require("../service/userService");
const helper = require("../helper/helper");
const { cid, redirect_uri } = require("../config");
const { createToken } = require("../middleware/jwt");
const { validate } = require("../utils/validate");
module.exports = {
  async createUser(req, res) {
    const { username, phoneNumber } = req.body;
    const { isError, error } = validate(req);
    try {
      if (isError) {
        return helper.errorMessage({
          res,
          errCode: "UserValidateError",
          error,
        });
      }
      //判断用户是否存在
      const user = await userService.findByUsername(username);
      const phone = await userService.findByPhoneNumber(phoneNumber);
      if (user) {
        return helper.errorMessage({ res, errCode: "CreateUserDBError" });
      }
      if (phone) {
        return helper.errorMessage({
          res,
          errCode: "CellphoneRegisteredError",
        });
      }
      //创建用户
      const result = await userService.createUserService(req.body);
      return helper.successMessage({ res, data: result, msg: "创建用户成功" });
    } catch (error) {
      return helper.errorMessage({ res });
    }
  },
  async userLogin(req, res) {
    const { username, password } = req.body;
    const { isError, error } = validate(req);
    if (isError) {
      return helper.errorMessage({ res, errCode: "UserValidateError", error });
    }
    const user = await userService.findByUsernameAndPassword({
      username,
      password,
    });
    if (user) {
      const token = await createToken({
        username: user.username,
        id: user._id,
        _id: user._id,
      });
      user._doc.token = token;
      return helper.successMessage({ res, data: user, msg: "登录成功" });
    } else {
      return helper.errorMessage({ res, errCode: "UserLoginError" });
    }
  },
  async oauthLogin(req, res) {
    const url = `https://gitee.com/oauth/authorize?client_id=${cid}&redirect_uri=${redirect_uri}&response_type=code`;
    res.redirect(url);
  },
  async giteeCallback(req, res) {
    const { code } = req.query;
    // try {
    const token = await userService.loginByGitee(code);
    return res.render("success.nj", { token });
    // } catch (error) {
    //   return helper.errorMessage({ res });
    // }
  },
  //修改用户信息
  async updateUser(req, res) {
    const { id } = req.query;
    const userId = req.info.id;
    //判断权限
    if (id !== userId) {
      return helper.errorMessage({ res, errCode: "UserNotModifyError" });
    }
    try {
      const data = await userService.updateUserService({ id, ...req.body });
      return helper.successMessage({ res, data, msg: "修改用户信息成功" });
    } catch (error) {
      return helper.errorMessage({ res });
    }
  },
  //删除用户
  async deleteUser(req, res) {
    const { id } = req.query;
    const userId = req.info.id;
    //判断权限
    if (id !== userId) {
      return helper.errorMessage({ res, errCode: "UserNotModifyError" });
    }
    try {
      const data = await userService.deleteUserService(id);
      return helper.successMessage({ res, data, msg: "删除用户成功" });
    } catch (error) {
      return helper.errorMessage({ res });
    }
  },
  //获取用户信息
  async getUserInfo(req, res) {
    try {
    const data = await userService.getUserInfoService(req.info.id);
    return helper.successMessage({ res, data, msg: "获取用户信息成功" });
    } catch (error) {
      return helper.errorMessage({ res });
    }    
  }
};
